<?php

namespace app\admin\controller;

use think\Controller;
use think\Error;
use think\Request;
use app\common\model\Admin as AdminModel;
use app\common\validate\Admin as AdminValidate;
use app\common\server\Code;
use think\Facade\Session;

class Login extends Controller
{

    /**
     * 后台登录
     * @param Request $request
     * @return string|\think\response\Json
     */
    public function login(Request $request)
    {
        if(!$request->isPost()){
            return $this->fetch('login');
        }
        $params = $request->post();
        $validate = new AdminValidate;
        $validateResult = $validate->scene('login')->check($params);
        if(!$validateResult) return apiReturn(Code::$error,$validate->getError());
        $result = AdminModel::login($params['username'],$params['password']);
        if(!$result) return apiReturn(Code::$error,'用户名或密码错误');
        return apiReturn();
    }

    //退出登录
    public function logout(){
        Session::clear();
        $this->redirect(url(config('admin.login_url')));
    }

}
